<?php

namespace app\api\service\ai;

use app\api\model\AiCompletionsDetail;
use think\Env;


class Yiyan extends Base
{
    protected $type = 'yiyan';

    protected $completionsUrl = 'https://qianfan.baidubce.com/v2/chat/completions';

    public function completions(string $id, string $model, string $content, array $img = [], $deepThink = false, $webSearch = false): void
    {
        if ($img) {
            $content = [['type' => 'text', 'text' => $content]];
            foreach ($img as $val) {
                $content[] = ['type' => 'image_url', 'image_url' => ['url' => $val]];
            }
        }
        $apiKey = Env::get('yiyan.apikey');
        if ($webSearch) {
            $this->completionsRequest(
                $id,
                $content,
                $model,
                $apiKey,
                function ($messages) use ($model) {
                    return json_encode(
                        [
                            'model'    => $model,
                            'messages' => $messages,
                            'stream'   => true,
                            'extra_body'    => [
                                'web_search' => [
                                    'enable' => true,
                                    'enable_trace' => true
                                ]
                            ]
                        ]
                    );
                }
            );
        } else {
            $this->completionsRequest($id, $content, $model, $apiKey);
        }
    }
}

